package com.zyw.customcalendar;

import com.zyw.customcalendar.dao.CalendarSQLite;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class AppApplication extends Application{

	public static int DEVICE_WIDTH = 1;
	public static int DEVICE_HEIGHT = 1;
	public static CalendarSQLite sqlite;
	public static AppApplication instance;
	private SharedPreferences mSharePre;

	
	public static AppApplication getInstance(){
		if(null == instance) {
            instance = new AppApplication();
        }
        return instance;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		InitData();
		super.onCreate();

		instance = this;
		mSharePre = getSharedPreferences("calendar", MODE_PRIVATE);
	}

	public void saveSharePreferences(String name, Object value){
		if(mSharePre == null){
			mSharePre = getApplicationContext().getSharedPreferences("calendar", MODE_PRIVATE);
		}

		SharedPreferences.Editor meditor = mSharePre.edit();
		if(value instanceof String){
			meditor.putString(name, (String)value);
		}
		if(value instanceof Integer){
			meditor.putInt(name, (Integer) value);
		}
		if(value instanceof Float){
			meditor.putFloat(name, (float) value);
		}
		meditor.apply();
	}

	public Object getSharePreferencesValue(String name, Object defaultValue){
		if(mSharePre == null){
			mSharePre = getApplicationContext().getSharedPreferences("calendar", MODE_PRIVATE);
		}

		if(defaultValue instanceof String){
			return mSharePre.getString(name, (String)defaultValue);
		}
		if(defaultValue instanceof Integer){
			return mSharePre.getInt(name, (Integer) defaultValue);
		}
		if(defaultValue instanceof Float){
			return mSharePre.getFloat(name, (Float) defaultValue);
		}
		return null;
	}
	
	private void InitData(){
		//instance = this;
		//��ʼ����ݿ�
		sqlite = new CalendarSQLite(this, "calendar_db3", null, 1);
		
		WindowManager windowManager = (WindowManager) this  
                .getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics metric = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(metric);
		DEVICE_WIDTH = metric.widthPixels; // ��Ļ��ȣ����أ�
		DEVICE_HEIGHT = metric.heightPixels; // ��Ļ�߶ȣ����أ�
	}
}
